package top.heue.epro.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.EditText;

import java.io.InputStream;
import java.lang.reflect.Method;

public class NormalUtil {

    //裁剪图片
    public static Bitmap getFitImage(Context context, InputStream is) {

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        wm.getDefaultDisplay().getSize(point);
        int width = point.x;
        int height = point.y;
        Display display = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        try {
            Class c = Class.forName("android.view.Display");
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, dm);
            height = dm.heightPixels;
        } catch (Exception e) {
            //LOG.i("获取真实高失败");
        }
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        int imgWidth = bitmap.getWidth();
        int imgHeight = bitmap.getHeight();
        float fo, fn;
        fo = (float) width / height;
        fn = (float) imgWidth / imgHeight;
        if (fo > fn) {//屏比例更大，即更宽，高为准
            imgHeight = Math.round(imgWidth * fo);
        } else {//更小，即更高，高为准
            imgWidth = Math.round(imgHeight * fo);
            //LOG.i("宽高", "" + imgWidth + "-" + imgHeight);
        }
        bitmap = Bitmap.createBitmap(bitmap, 0 , 0, imgWidth, imgHeight);
        return bitmap;
    }

    //隐藏键盘
    public static void disableShowSoftInput(EditText editText) {
        Class<EditText> cls = EditText.class;
        Method method;
        try {
            method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            method.setAccessible(true);
            method.invoke(editText, false);
        } catch (Exception e) {
            // TODO: 2018/8/27 处理错误 
        }
    }

    //显示对话框
    public static void showDialog(final Activity context, String title, String content) {
        AlertDialog dialog=new AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(content)
            .setPositiveButton("手动重启", (dia, which) -> NormalUtil.stopApp(context))
            .create();
        dialog.show();
    }

    //版本获取
    public static String getVersion(Context context) {
        //LOG.i("获取版本");
        String version = "";
        PackageManager packageManager = context.getPackageManager(); 
        // getPackageName()是当前类的包名，0是获取版本信息 
        try {
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0); 
            version = packInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }

    //文字颜色
    public static SpannableString wordColor(String s, Boolean b) {
        SpannableString spanString = new SpannableString(s);
        ForegroundColorSpan span;
        if (b) {
            span = new ForegroundColorSpan(Color.parseColor("#81C784"));
        } else {
            span = new ForegroundColorSpan(Color.RED);
        }
        spanString.setSpan(span, 0, s.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return spanString;
    }

    //结束程序
    public static void stopApp(Activity activity) {
        //LOG.i("结束程序");
        activity.finish();
        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    //LOG.i("线程暂停错误", e);
                }
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        }.start();
    }

    //重启程序
    /*
    public static void restartApp(Context context) {
        Intent intent = context.getPackageManager()
            .getLaunchIntentForPackage(context.getPackageName());
        PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, 0);
        AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis(), restartIntent);
        System.exit(0);
    }*/
}
